package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.Page;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.pojo.TbContentCategory;
import com.dongdong.service.ConCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("conCateController")
public class ConCateController {
    @Reference(timeout = 50000)
    private ConCategoryService conCategoryService;

    @RequestMapping("toCategory")
    public String toCategory(){

        return "admin/content_category";
    }
    @RequestMapping("categoryList")
    @ResponseBody
    public Result categoryList(String name,@RequestParam(defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "3")Integer pageSzie) {
      try{
          Page<TbContentCategory> list =  conCategoryService.selectConCategoryList(name,pageNum,pageSzie);
          return Result.respone(ResultEmnu.SUCCESS).setData(list);
      }catch (Exception e){
          e.printStackTrace();
          return Result.respone(ResultEmnu.ERROR);
      }
    }
    @RequestMapping("increase")
    @ResponseBody
    public Result addCategory(TbContentCategory tbContentCategory){
        try{
           int i = conCategoryService.insert(tbContentCategory);
            return Result.respone(ResultEmnu.SUCCESS);
        }catch (Exception e){
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id")Long id){
        TbContentCategory category = conCategoryService.selectById(id);
        return Result.respone(ResultEmnu.SUCCESS).setData(category);
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbContentCategory tbContentCategory){
       int i = conCategoryService.updateInfo(tbContentCategory);
        if (i > 1){
           return Result.respone(ResultEmnu.SUCCESS);
       }else {
           return Result.respone(ResultEmnu.ERROR);
       }
    }
    @RequestMapping("deleteBatch/{ids}")
    @ResponseBody
    public Result deleteBatch(@PathVariable("ids")Long[] ids){
        int i = conCategoryService.deleteBatch(ids);
        System.out.println(Result.respone(ResultEmnu.SUCCESS));
        if (i > 1){
            return Result.respone(ResultEmnu.SUCCESS);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }
}
